#ifndef OMG_DDS_WAITSET_HPP_
#define OMG_DDS_WAITSET_HPP_

/* Copyright (c) 2009-2010, Real-Time Innovations, Inc.
 * Copyright (c) 2010, Object Management Group, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * - Neither the names of the above copyright holders nor the names of their
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL A COPYRIGHT HOLDER OR CONTRIBUTOR BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */


/**
 * A WaitSet object allows an application to wait until one or more of 
 * the attached Condition objects has a trigger_value of TRUE or else 
 * until the timeout expires.
 * A WaitSet is not necessarily associated with a single DomainParticipant 
 * and could be used to wait on Condition objects associated with different 
 * DomainParticipant objects.
 */
template <typename DELEGATE>
class tdds::core::WaitSet : public dds::core::Reference<DELEGATE>
{
    OMG_DDS_REFERENCE_TYPE(WaitSet)

public:
    typedef std::vector<dds::core::Condition> ConditionSeqType;

public:
    static WaitSet create();
    void close();

public:
    void wait(const dds::core::Duration &timeout =
                    dds::core::Duration::infinite());

    /**
     * This operation allows an application thread to wait for the occurrence
     * of certain conditions. If none of the conditions attached to the 
     * WaitSet have a trigger_value of TRUE, the wait operation will block 
     * suspending the calling thread.
     * The result of the wait operation is the list of all the attached 
     * conditions that have a trigger_value of TRUE (i.e., the conditions 
     * that unblocked the wait).
     * The wait operation takes a timeout argument that specifies the maximum 
     * duration for the wait. It this duration is exceeded and none of 
     * the attached Condition objects is true, wait will continue and the
     * returned ConditionSeq will be empty.
     * It is not allowed for more than one application thread to be waiting 
     * on the same WaitSet. If the wait operation is invoked on a WaitSet that
     * already has a thread blocking on it, the operation will raise  
     * immediately an exception PreconditionNotMet.
     *
     * @param timeout the maximum amount of time for which the wait
     * should block while waiting for a condition to be triggered.
     *
     * @raise PreconditionNotMetException when multiple thread try to invoke
     *        the method concurrently.
     *
     * @return a vector containing the triggered conditions
     *
     */
    ConditionSeqType&
    wait(ConditionSeqType& triggered,
         const dds::core::Duration& timeout =
                dds::core::Duration::infinite());

    /**
     * Attaches a Condition to the WaitSet. It is possible to attach a 
     * Condition on a WaitSet that is currently being waited upon 
     * (via the wait operation). In this case, if the Condition has a 
     * trigger_value of TRUE, then attaching the condition will unblock 
     * the WaitSet. Adding a Condition that is already attached to the WaitSet
     * has no effect. 
     *
     * @param cond the condition to be attached to this waitset.
     */
    void attach_condition(const dds::core::Condition& cond);

    /**
     * A synonym for attach_condition.
     */
    WaitSet& operator+=(const dds::core::Condition& cond);

    /**
     * Detaches a Condition from the WaitSet. If the Condition was not 
     * attached to the WaitSet, the operation will return false.
     *
     * @param cond the condition to detach from this WaitSet
     * @return true if the condition was found and detached, false if the 
     *         condition was not part of the WaitSet.
     */
    void detach_condition(const dds::core::Condition& cond);

    /**
     * A synonym for detach_condition.
     */
    WaitSet& operator-=(const dds::core::Condition& cond);

    /**
     * This operation retrieves the list of attached conditions.
     *
     * @return the list of attached conditions.
     */
    ConditionSeqType&
    get_conditions(ConditionSeqType& conditions) const;

    ConditionSeqType get_conditions() const;
};


#endif // !defined(OMG_DDS_WAITSET_HPP_)
